<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/30 0030
 * Time: 14:28
 */
namespace Admin\Controller;

use Think\Controller;
class CommonController extends Controller {
    var $product;
    var $advert;
    var $gallery;
    function __construct()
    {
        parent::__construct();
        $this->check_login();
        $this->product = M('Product');
        $this->advert =M('Advert');
        $this->gallery = M('Gallery');
        $this->categories = $this->all_categories();
        //侧边栏统计
        $count = [];
        $count['products'] = $this->product->count();
        $count['adverts'] = $this->advert->count();
        $count['trashs'] = $this->product->where("status  = 0")->count();
        $this->assign("count",$count);
    }
    public function all_categories()
    {
        $Category = M('Category');
        $categories = $Category->where("parent_id = 0")->select();
        foreach ($categories as $key => $value) {
            $children = $Category->where("parent_id = $value[id]")->select();
            $categories[$key]['children'] = $children;
        }
        return $categories;
    }

    /***
     * 检查用户是否登录
     */
    public function check_login()
    {
        if(!$_SESSION['user']){
            $this->error("此用户未登录，请登陆后再试",U('User/login'));
        }
    }
}